package com.ljkj.cordial.chat.util;

import android.app.Activity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.support.annotation.Nullable;
import android.view.Surface;
import android.view.View;
import android.view.ViewGroup;

import com.zhaoss.weixinrecorded.activity.RecordedActivity;

public class WxVideoPlayUtil {
    private static class Build{
        public static WxVideoPlayUtil instance=new WxVideoPlayUtil();
    }
    public static WxVideoPlayUtil getInstance(){
        return Build.instance;
    }
    //    private MediaPlayer mMediaPlayer;
    public void startRecord(Activity activity, String videoDir, String picDir) {
        Intent intent = new Intent(activity, RecordedActivity.class);
        intent.putExtra("video_output", videoDir);
        intent.putExtra("pic_output", picDir);
        activity.startActivityForResult(intent, 1);
//        if(mMediaPlayer != null){
//            mMediaPlayer.stop();
//            mMediaPlayer.release();
//            mMediaPlayer = null;
//        }
    }

    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data,OnVideoCallback onVideoCallback) {
        if (resultCode == Activity.RESULT_OK && data != null) {
            if (requestCode == 1) {
                int dataType = data.getIntExtra(RecordedActivity.INTENT_DATA_TYPE, RecordedActivity.RESULT_TYPE_VIDEO);
                if (dataType == RecordedActivity.RESULT_TYPE_VIDEO) {
                    String videoPath = data.getStringExtra(RecordedActivity.INTENT_PATH);
//                    tv_path.setText("视频地址: "+videoPath);
                    if (onVideoCallback != null) {
                        onVideoCallback.onVideoCall(videoPath);
                    }
                } else if (dataType == RecordedActivity.RESULT_TYPE_PHOTO) {
                    String photoPath = data.getStringExtra(RecordedActivity.INTENT_PATH);
//                    tv_path.setText("图片地址: "+photoPath);
                    if (onVideoCallback != null) {
                        onVideoCallback.onPhotoCall(photoPath);
                    }
                }
            }
        }
    }

    public OnVideoCallback onVideoCallback;

    public void setOnVideoCallback(OnVideoCallback onVideoCallback) {
        this.onVideoCallback = onVideoCallback;
    }

    public interface OnVideoCallback {
        void onVideoCall(String videoPath);

        void onPhotoCall(String photoPath);
    }
//    private void playVideo(String videoPath){
//
//        try {
//            mMediaPlayer = new MediaPlayer();
//            mMediaPlayer.setDataSource(videoPath);
//            mMediaPlayer.setSurface(new Surface(textureView.getSurfaceTexture()));
//            mMediaPlayer.setLooping(true);
//            mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
//                @Override
//                public void onPrepared(MediaPlayer mp) {
//                    mp.start();
////                    float ratio = mp.getVideoWidth()*1f/mp.getVideoHeight();
////                    int width = textureView.getWidth();
////                    ViewGroup.LayoutParams layoutParams = textureView.getLayoutParams();
////                    layoutParams.height = (int) (width/ratio);
////                    textureView.setLayoutParams(layoutParams);
//                }
//            });
//            mMediaPlayer.prepareAsync();
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//    }
}
